package com.example.yls.demoa;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yls on 2018/11/13.
 */

public class StuentAdapter extends BaseAdapter {
    private Context context;
    private List<Student> studentArrayList;
    private IOnDataChangeListener listener;

    public StuentAdapter(Context context, IOnDataChangeListener listener, List<Student> studentArrayList) {
        this.context = context;
        this.listener = listener;
        this.studentArrayList = studentArrayList;
    }

    @Override
    public int getCount() {
        return studentArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return studentArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if(view == null){
            view = LayoutInflater.from(context).inflate(R.layout.list_item, viewGroup, false);

            StudentViewHolder holder = new StudentViewHolder();
            holder.txtNo = view.findViewById(R.id.txt_no);
            holder.txtName = view.findViewById(R.id.txt_name);
            holder.txtAge = view.findViewById(R.id.txt_age);
            holder.item_del = view.findViewById(R.id.item_del);

            view.setTag(holder);
        }

        StudentViewHolder holder = (StudentViewHolder) view.getTag();
        final Student s = studentArrayList.get(i);

        holder.txtNo.setText(s.getStuno());
        holder.txtName.setText(s.getName());
        holder.txtAge.setText(String.valueOf(s.getAge()));

        holder.item_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.delStudent(s.getStuno());
            }
        });

        return view;
    }

    public void changeData(List<Student> studentArrayList) {
        this.studentArrayList = studentArrayList;
        notifyDataSetChanged();
    }
}
